﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Diagnostics;

namespace Meraz.Core.Parsers
{
    public class RegexFacebookParser
    {
        static Regex TITLE_REGEX = new Regex(@".*<title>(?<target>.+) | Facebook</title>");
        static Regex TOTAL_REGEX = new Regex(@"<div class=""summary"">.*\b(?<total>\d+)[^\d]</div>", RegexOptions.Singleline);
        static Regex URL_REGEX = new Regex(@"<link\s*rel=['""]alternate['""]\s*media=['""]handheld['""]\s*href=['""](?<target>.+)['""]\s*/>", RegexOptions.Singleline);
        static Regex MESSAGE_REGEX = new Regex(@"<span\s*class=['""]messageBody['""]\s*>(?<target>.+?)</span>", RegexOptions.Singleline);
        static Regex ACTOR_REGEX = new Regex(@"<div class=['""]actorName actorDescription['""]>\s*(?<target>.+?)\s*</div>");
        static Regex POSTDATE_REGEX = new Regex(@"(?<!Post Deleted)</h6>.+?<abbr.+?data-date=['""](?<target>.+?)['""]");
        static Regex LINK_REGEX = new Regex(@"<a.+?href[^<>]+>\s*(?<target>.+?)\s*</a>");
        static Regex UID_REGEX = new Regex(@"uid=(?<target>\d\d\d\d\d\d\d\d\d\d\d)");
        public static string GetUrl(string content)
        {
            return GetString(content, URL_REGEX, "target");
        }
        public static string GetUID(string content)
        {
            return GetString(content, UID_REGEX, "target");
        }
        public static string GetTitle(string content)
        {
            return GetString(content, TITLE_REGEX, "target");
        }

        public static List<string> GetPostDate(string content)
        {
            return GetStrings(content, POSTDATE_REGEX, "target");
        }
        public static List<string> GetMessages(string content)
        {
            return GetStrings(content, MESSAGE_REGEX, "target");
        }
        public static List<string> GetActors(string content)
        {
            return GetStrings(content, ACTOR_REGEX, "target");
        }
        public static List<string> GetLinks(string content)
        {
            return GetStrings(content, LINK_REGEX, "target");
        }
        public static List<T> GetObjects<T>(string content, Regex regex)
        {
            List<T> result = new List<T>();
            Type d1 = typeof(T);

            MatchCollection matches = regex.Matches(content);
            foreach (Match match in matches)
            {
                try
                {

                    ConstructorInfo ctor = d1.GetConstructor(System.Type.EmptyTypes);
                    if (ctor != null)
                    {
                        T instance = (T)ctor.Invoke(null);
                        foreach (string groupName in regex.GetGroupNames())
                        {
                            PropertyInfo prop = d1.GetProperty(groupName);
                            if (prop == null) continue;
                            string value = match.Groups[groupName].Value;
                            if (string.IsNullOrWhiteSpace(value)) continue;
                            prop.SetValue(instance, value, null);
                        }
                        result.Add(instance);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
            return result;
        }

        public static T GetObject<T>(string content, Regex regex)
        {
            Type d1 = typeof(T);

            Match match = regex.Match(content);
                try
                {

                    ConstructorInfo ctor = d1.GetConstructor(System.Type.EmptyTypes);
                    if (ctor != null)
                    {
                        T instance = (T)ctor.Invoke(null);
                        foreach (string groupName in regex.GetGroupNames())
                        {
                            PropertyInfo prop = d1.GetProperty(groupName);
                            if (prop == null) continue;
                            string value = match.Groups[groupName].Value;
                            if (string.IsNullOrWhiteSpace(value)) continue;
                            prop.SetValue(instance, value, null);
                        }
                        return instance;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            
            return default(T);
        }
        public static List<string> GetStrings(string content, Regex regex, string groupName)
        {
            List<string> strings = new List<string>();
            MatchCollection matches = regex.Matches(content);
            foreach (Match match in matches){
                if (match.Groups[groupName] != null)
                    strings.Add(match.Groups[groupName].Value);
            }
            return strings;
        }
        public static string GetString(string content, Regex regex, string groupName)
        {
            string result = string.Empty;
            Match match = regex.Match(content);
            if (match.Success)
            {
                if (match.Groups[groupName] != null)
                    result = match.Groups[groupName].Value;
            }
            return result;
        }
        public static int GetTotal(string content)
        {
            string result = string.Empty;
            Match match = TOTAL_REGEX.Match(content);
            if (match.Success)
            {
                if (match.Groups["total"] != null)
                    result = match.Groups["total"].Value;
            }
            int value = 0;
            int.TryParse(result, out value);
            return value;
        }
        public static string RemoveHtml(string content)
        {
            return Regex.Replace(content, @"<[^>]+>", "");
        }
    }
}
